//
//  TSCMyPhotoDialog
//  Tsc_IOS
//
//  Created by Rio on 15-9-2.
//  Copyright (c) 2015年 idream. All rights reserved.
//


#import "TSCMyPhotoDialog.h"
#import "TSCMyUtil.h"
#import "TSCConstants.h"
#import "TSCMyImageUtil.h"


@implementation TSCMyPhotoDialog {
    UIView * _gestureBG; // 背景图层，用来接收用户点击事件，从而关闭dialog
    UIImageView * _ivPhoto; // 显示相片的UIImageView
}


#pragma mark 初始化

- (id)init {
    // 以满屏创建
    self  = [super init];
    if (self) {
        [self initMyDialog];
    }
    return self;
}

- (id) initWithViewController:(UIViewController *)vc image:(UIImage *)image {
    self = [self init];
    if (self) {
        _viewController = vc;
        _image = image;
    }
    return self;
}

- (void) initMyDialog {
    // 注：剩下两个属性放在显示时再初始化
}

#pragma mark 私有方法

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

/* private */ - (void) layoutUI {
    
    // 先将自身添加到当前界面中（因为后续要设置约束）
    // 方式一：加入全局窗体。特点：当前vc消失后，dialog依然在
    //    [[[UIApplication sharedApplication].delegate window]addSubview:self];
    // 方式二：加入当前窗体。特点：当前vc消失后，dialog也不在了
    [_viewController.view addSubview:self];
    
    // 添加modal背景
    _gestureBG = [[UIView alloc]init];
    [_gestureBG setBackgroundColor:COLOR_MT_DIALOG_MODAL];
    [self addSubview:_gestureBG];
    
    // modal注册事件
    _gestureBG.userInteractionEnabled = YES;
    UITapGestureRecognizer * tgrModalBGClicked = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modalBGClicked)];
    [_gestureBG addGestureRecognizer:tgrModalBGClicked];
    
    // 计算合适的显示视窗尺寸
    CGFloat winWH = MIN(UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT) / 5 * 4; // 屏幕宽高最小值的4/5（剩下的1/5是图片与屏幕边界的padding）
    UIImage * scaledImage = [TSCMyImageUtil scaleImage:_image windowW:winWH windowH:winWH zoomMode:ZoomModeOneAxis]; // 单轴缩放方式
    
    // 创建UIImageView
    _ivPhoto = [[UIImageView alloc]initWithImage:scaledImage];
    [self addSubview:_ivPhoto];
    
    // 添加约束
    // self
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)]];
    // bg
    _gestureBG.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_gestureBG]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_gestureBG)]];
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_gestureBG]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_gestureBG)]];
    // UIImageView
    _ivPhoto.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f].active = YES;
    [NSLayoutConstraint constraintWithItem:_ivPhoto attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f].active = YES;
    
}


/* private */ - (void) modalBGClicked {
    // 点击背景时自动关闭
    [self dismiss];
}

#pragma mark 公有方法

- (void) show {
    // 先检查是否设置了vc，因为外部使用者有可能直接使用init()来初始化，从而忘记设置vc
    NSAssert(_viewController != nil, @"[E]%s: MyDialog调用show()方法时，viewController为nil！请先设置viewController！", __FUNCTION__);
    NSAssert(_image != nil, @"[E]%s: MyDialog调用show()方法时，image为nil！请先设置image！", __FUNCTION__);
    
    // 先设置透明
    self.alpha = 0.0f;
    
    // 界面布局
    [self layoutUI];
    
    // 动画显示
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1.0f;
    }];
}

- (void) dismiss {
    // 动画显示
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        // 从主窗体中移除
        [self removeFromSuperview];
    }];
}


@end
